package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicSupplierDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicVarietyDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicVarietyService;

@Tag(name = "管理后台 - 品种海关编码")
@RestController
@RequestMapping("/contract/basic-variety")
@Validated
public class BasicVarietyController {

    @Resource
    private BasicVarietyService basicVarietyService;

    @PostMapping("/create")
    @Operation(summary = "创建品种海关编码")
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:create')")
    public CommonResult<Long> createBasicVariety(@Valid @RequestBody BasicVarietySaveReqVO createReqVO) {
        return success(basicVarietyService.createBasicVariety(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新品种海关编码")
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:update')")
    public CommonResult<Boolean> updateBasicVariety(@Valid @RequestBody BasicVarietySaveReqVO updateReqVO) {
        basicVarietyService.updateBasicVariety(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除品种海关编码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:delete')")
    public CommonResult<Boolean> deleteBasicVariety(@RequestParam("id") Long id) {
        basicVarietyService.deleteBasicVariety(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得品种海关编码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:query')")
    public CommonResult<BasicVarietyRespVO> getBasicVariety(@RequestParam("id") Long id) {
        BasicVarietyDO basicVariety = basicVarietyService.getBasicVariety(id);
        return success(BeanUtils.toBean(basicVariety, BasicVarietyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得品种海关编码分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:query')")
    public CommonResult<PageResult<BasicVarietyRespVO>> getBasicVarietyPage(@Valid BasicVarietyPageReqVO pageReqVO) {
        PageResult<BasicVarietyDO> pageResult = basicVarietyService.getBasicVarietyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicVarietyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出品种海关编码 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-variety:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicVarietyExcel(@Valid BasicVarietyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicVarietyDO> list = basicVarietyService.getBasicVarietyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "品种海关编码.xls", "数据", BasicVarietyRespVO.class,
                        BeanUtils.toBean(list, BasicVarietyRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得品种海关编码列表")
    public CommonResult<List<BasicVarietyDO>> getBasicVarietyList(@Valid BasicVarietyDO basicVarietyDO) {
        List<BasicVarietyDO> listResult = basicVarietyService.list(Wrappers.lambdaQuery(basicVarietyDO));
        return success(listResult);
    }

}